package hotciv.decorators;

import java.io.BufferedWriter;
import java.io.FileWriter;

import hotciv.framework.City;
import hotciv.framework.Game;
import hotciv.framework.GameObserver;
import hotciv.framework.HotCivFactory;
import hotciv.framework.Player;
import hotciv.framework.Position;
import hotciv.framework.Tile;
import hotciv.framework.Unit;
import hotciv.standard.GameImpl;

public class GameLogDecorator implements Game 
{
	
	private Game civGame;
		
		public GameLogDecorator(HotCivFactory thisFactory)
		{		
			civGame=new GameImpl(thisFactory);
		}

		@Override
		public void addObserver(GameObserver observer) 
		{
			civGame.addObserver(observer);
		}
	
		@Override
		public void changeProductionInCityAt(Position p, String unitType) 
		{
			printToLogfile(civGame.getPlayerInTurn()+" changes production in city at "+p.toString()+" to "+unitType);
			civGame.changeProductionInCityAt(p, unitType);
		}

		@Override
		public void changeWorkForceFocusInCityAt(Position p, String balance) 
		{	
			if(balance.equals("apple"))
			{
				printToLogfile(civGame.getPlayerInTurn()+" changes work force focus in city at "+p.toString()+" to foodFocus");
			}
			if(balance.equals("hammer"))
			{
				printToLogfile(civGame.getPlayerInTurn()+" changes work force focus in city at "+p.toString()+" to productionFocus");
			}
			civGame.changeWorkForceFocusInCityAt(p, balance);
		}

		@Override
		public void endOfTurn() 
		{
			printToLogfile(civGame.getPlayerInTurn()+" ends turn.");
			civGame.endOfTurn();
		}

		@Override
		public int getAge()
		{
			return civGame.getAge();
		}

		@Override
		public City getCityAt(Position p) {
			return civGame.getCityAt(p);
		}

		@Override
		public Player getPlayerInTurn() {
			return civGame.getPlayerInTurn();
		}

		@Override
		public Tile getTile(Position p) {
			return civGame.getTile(p);
		}

		@Override
		public Unit getUnitAt(Position p) {
			return civGame.getUnitAt(p);
		}

		@Override
		public Player getWinner() {
			
			if(civGame.getWinner()!=null)
			{
				printToLogfile(civGame.getWinner().toString()+" has won!");
			}
			else
			{
				printToLogfile("No winner yet!");
			}
			return civGame.getWinner();
		}
		
		@Override
		public boolean moveUnit(Position from, Position to)
		{
			String unit = civGame.getUnitAt(from).getTypeString();
				if(civGame.moveUnit(from, to))
				{
					printToLogfile(civGame.getPlayerInTurn()+" moves "+unit+" from "+from.toString()+" to "+to.toString());
					return true;	
				}
				else
				{
					printToLogfile(civGame.getPlayerInTurn()+" try to move "+unit+" from "+from.toString()+" to "+to.toString()+" but it's illegal");
					return false;
				}
		}

		@Override
		public void performUnitActionAt(Position p) 
		{
			civGame.performUnitActionAt(p);	
		}
		
		protected void printToLogfile(String s)
		{
			try
			{
				BufferedWriter writer = new BufferedWriter(new FileWriter("logfile.txt", true)); 
			 
				writer.write(s);
				writer.newLine();
			 
				writer.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}

		@Override
		public void changeTileFocus(Position position) {}		
}
